package com.server.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_ByteBuff;
import com.sandking.tools.SK_Plus;
import java.util.List;
import com.server.db.imp.YxjnImp;
import java.util.HashMap;
import com.server.db.imp.YhyxImp;

/**
 * 英雄技能
 */
public class Yxjn {

	public static final String TABLE_NAME = "英雄技能";
	public static final String CLASS_NAME = "Yxjn"; 
	static final int MAX_GET = Short.MAX_VALUE;
	
	//索引名称
	public static final String  Id_IndexName = "id";
	public static final String  Yhyx_id_IndexName = "yhyx_id";
	
	//对象版本号	
	private int _v = 0;
	public int _nextVer(){
		if(_v > MAX_GET) _v = 0;
		return ++_v;
	}
	
	public int _getVer(){
		return _v;
	}
	
	//是否是源对象
	private boolean _source = false;
	
	public boolean is_source() {
		return _source;
	}

	public void set_source(boolean _source) {
		this._source = _source;
	}
	
	//Cache 中的索引
	private String  id_Unique = null;
	private String  yhyx_id_Index = null;
	public Yxjn updatePrimaryIndex(){
		return updatePrimaryIndex(this);
	}
	
	public static List<Yxjn> updatePrimaryIndex(List<Yxjn> yxjns){
		for(Yxjn yxjn : yxjns){
			updatePrimaryIndex(yxjn);	
		}
		return yxjns;
	}
	
	public static Yxjn updatePrimaryIndex(Yxjn yxjn){
		String field = SK_Plus.b(yxjn.getId()).e();
		yxjn.setId_Unique(field);
		
		field = SK_Plus.b(yxjn.getYhyx_id()).e();
		yxjn.setYhyx_id_Index(field);
		
		return yxjn;
	}
	
	
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/* 可能需要补救的字段 */
	private SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	
	public SK_ByteBuff getSK_ByteBuff(){
		return this.byteBuff;
	}
	
	/** id */
	private int id;
	
	/** 用户英雄_id */
	private int yhyx_id;
	
	/** 技能id */
	private int jnid;
	
	/** 技能lvl */
	private int jnlvl;
	
	
	public Yxjn() {
		super();
		this._source = true;
	}
	
	public Yxjn(int id, int yhyx_id, int jnid, int jnlvl) {
		super();
		this.id = id;
		this.yhyx_id = yhyx_id;
		this.jnid = jnid;
		this.jnlvl = jnlvl;
		this._source = true;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public void setUpdateColumns(Map<String, Object> updateColumns) {
		this.updateColumns = updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		changeSetId(id);
		if(!this._source){
			byteBuff.writeInt(1);
			byteBuff.writeInt(id);
		}
	}
	
	private void changeSetId(int id) {
		this.id = id;
		if(!this._source){
			addUpdateColumn("id",id);
		}
	}
	
	public void changeIdWith(int id){
		this.id += id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(2);
			byteBuff.writeInt(id);
		}
	}
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(3);
			byteBuff.writeInt(id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(4);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
		}
	}
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(5);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getYhyx_id() {
		return yhyx_id;
	}
	
	public void setYhyx_id(int yhyx_id) {
		changeSetYhyx_id(yhyx_id);
		if(!this._source){
			byteBuff.writeInt(6);
			byteBuff.writeInt(yhyx_id);
		}
	}
	
	private void changeSetYhyx_id(int yhyx_id) {
		this.yhyx_id = yhyx_id;
		if(!this._source){
			addUpdateColumn("用户英雄_id",yhyx_id);
		}
	}
	
	public void changeYhyx_idWith(int yhyx_id){
		this.yhyx_id += yhyx_id;
		changeSetYhyx_id(this.yhyx_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(7);
			byteBuff.writeInt(yhyx_id);
		}
	}
	public void changeYhyx_idWithMin(int yhyx_id,int min){
		this.yhyx_id += yhyx_id;
		this.yhyx_id = this.yhyx_id < min ? min : this.yhyx_id;
		changeSetYhyx_id(this.yhyx_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(8);
			byteBuff.writeInt(yhyx_id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeYhyx_idWithMax(int yhyx_id,int max){
		this.yhyx_id += yhyx_id;
		this.yhyx_id = this.yhyx_id > max ? max : this.yhyx_id;
		changeSetYhyx_id(this.yhyx_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(9);
			byteBuff.writeInt(yhyx_id);
			byteBuff.writeInt(max);
		}
	}
	public void changeYhyx_idWithMaxMin(int yhyx_id,int max,int min){
		this.yhyx_id += yhyx_id;
		this.yhyx_id = this.yhyx_id < min ? min : this.yhyx_id;
		this.yhyx_id = this.yhyx_id > max ? max : this.yhyx_id;
		changeSetYhyx_id(this.yhyx_id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(10);
			byteBuff.writeInt(yhyx_id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getJnid() {
		return jnid;
	}
	
	public void setJnid(int jnid) {
		changeSetJnid(jnid);
		if(!this._source){
			byteBuff.writeInt(11);
			byteBuff.writeInt(jnid);
		}
	}
	
	private void changeSetJnid(int jnid) {
		this.jnid = jnid;
		if(!this._source){
			addUpdateColumn("技能id",jnid);
		}
	}
	
	public void changeJnidWith(int jnid){
		this.jnid += jnid;
		changeSetJnid(this.jnid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(12);
			byteBuff.writeInt(jnid);
		}
	}
	public void changeJnidWithMin(int jnid,int min){
		this.jnid += jnid;
		this.jnid = this.jnid < min ? min : this.jnid;
		changeSetJnid(this.jnid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(13);
			byteBuff.writeInt(jnid);
			byteBuff.writeInt(min);
		}	
	}
	public void changeJnidWithMax(int jnid,int max){
		this.jnid += jnid;
		this.jnid = this.jnid > max ? max : this.jnid;
		changeSetJnid(this.jnid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(14);
			byteBuff.writeInt(jnid);
			byteBuff.writeInt(max);
		}
	}
	public void changeJnidWithMaxMin(int jnid,int max,int min){
		this.jnid += jnid;
		this.jnid = this.jnid < min ? min : this.jnid;
		this.jnid = this.jnid > max ? max : this.jnid;
		changeSetJnid(this.jnid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(15);
			byteBuff.writeInt(jnid);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getJnlvl() {
		return jnlvl;
	}
	
	public void setJnlvl(int jnlvl) {
		changeSetJnlvl(jnlvl);
		if(!this._source){
			byteBuff.writeInt(16);
			byteBuff.writeInt(jnlvl);
		}
	}
	
	private void changeSetJnlvl(int jnlvl) {
		this.jnlvl = jnlvl;
		if(!this._source){
			addUpdateColumn("技能lvl",jnlvl);
		}
	}
	
	public void changeJnlvlWith(int jnlvl){
		this.jnlvl += jnlvl;
		changeSetJnlvl(this.jnlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(17);
			byteBuff.writeInt(jnlvl);
		}
	}
	public void changeJnlvlWithMin(int jnlvl,int min){
		this.jnlvl += jnlvl;
		this.jnlvl = this.jnlvl < min ? min : this.jnlvl;
		changeSetJnlvl(this.jnlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(18);
			byteBuff.writeInt(jnlvl);
			byteBuff.writeInt(min);
		}	
	}
	public void changeJnlvlWithMax(int jnlvl,int max){
		this.jnlvl += jnlvl;
		this.jnlvl = this.jnlvl > max ? max : this.jnlvl;
		changeSetJnlvl(this.jnlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(19);
			byteBuff.writeInt(jnlvl);
			byteBuff.writeInt(max);
		}
	}
	public void changeJnlvlWithMaxMin(int jnlvl,int max,int min){
		this.jnlvl += jnlvl;
		this.jnlvl = this.jnlvl < min ? min : this.jnlvl;
		this.jnlvl = this.jnlvl > max ? max : this.jnlvl;
		changeSetJnlvl(this.jnlvl);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(20);
			byteBuff.writeInt(jnlvl);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	
	//用户英雄_id
	public Yhyx getYhyxPkYhyx_id(){
		return YhyxImp.getById(yhyx_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	/**
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	*/
	public Yxjn clone(){
		Yxjn yxjn = new Yxjn();
		yxjn.setId(this.getId());
		yxjn.setYhyx_id(this.getYhyx_id());
		yxjn.setJnid(this.getJnid());
		yxjn.setJnlvl(this.getJnlvl());
		yxjn._v = _v;
		yxjn._source = false;
		return yxjn.updatePrimaryIndex(); 
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("yhyx_id", this.yhyx_id);
        result.put("jnid", this.jnid);
        result.put("jnlvl", this.jnlvl);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("用户英雄_id", this.yhyx_id);
        result.put("技能id", this.jnid);
        result.put("技能lvl", this.jnlvl);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	    byteBuff.writeInt(this.id);
	    byteBuff.writeInt(this.yhyx_id);
	    byteBuff.writeInt(this.jnid);
	    byteBuff.writeInt(this.jnlvl);
	    return byteBuff.toByte();
    }
    
    public byte[] toSKBytes(){
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
    	byteBuff.writeMap(toMap());
	    return byteBuff.toByte();
    }
    
     public static Yxjn createForBytes(byte[] _byte) throws Exception {
     	SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     	Yxjn yxjn = new Yxjn();
	    yxjn.id = byteBuff.readInt(null);
	    yxjn.yhyx_id = byteBuff.readInt(null);
	    yxjn.jnid = byteBuff.readInt(null);
	    yxjn.jnlvl = byteBuff.readInt(null);
	    return yxjn.updatePrimaryIndex();
     }
     
     public static Yxjn createForSKBytes(byte[] _byte){
     		SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     		@SuppressWarnings("unchecked")
     		Map<String,Object> map = byteBuff.readMap(null);
	     	Yxjn yxjn = Yxjn.createForMap(map);
		    return yxjn.updatePrimaryIndex();
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yxjn> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yxjn> yxjns = new ArrayList<Yxjn>();
		for (Map<String, Object> map : list) {
			yxjns.add(createForColumnNameMap(map));
		}
		return yxjns;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yxjn createForColumnNameMap(Map<String, Object> map){
    	Yxjn obj = new Yxjn();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yhyx_id = SK_Map.getInt("用户英雄_id", map);
	    obj.jnid = SK_Map.getInt("技能id", map);
	    obj.jnlvl = SK_Map.getInt("技能lvl", map);
        return obj.updatePrimaryIndex();
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yxjn> createForList(List<Map<String, Object>> list){
    	List<Yxjn> yxjns = new ArrayList<Yxjn>();
		for (Map<String, Object> map : list) {
			yxjns.add(createForColumnNameMap(map));
		}
		return yxjns;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yxjn createForMap(Map<String, Object> map){
    	Yxjn obj = new Yxjn();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yhyx_id = SK_Map.getInt("yhyx_id", map);
	    obj.jnid = SK_Map.getInt("jnid", map);
	    obj.jnlvl = SK_Map.getInt("jnlvl", map);
        return obj.updatePrimaryIndex();
    }
    
    public static Yxjn createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yxjn> createForJson(List<String> jsons){
    	List<Yxjn> yxjns = new ArrayList<Yxjn>();
    	for(String json : jsons){
    		yxjns.add(createForJson(json));
    	}
    	return yxjns;
    }
    
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Yxjn insert(){
    	return YxjnImp.insert(this);
    }
    /** 延迟更新数据库 */
    public Yxjn update(){
    	return YxjnImp.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YxjnImp.delete(this);
    }
    /** 即时插入数据库 */
    public Yxjn insertAndFlush(){
    	return YxjnImp.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yxjn updateAndFlush(){
    	return YxjnImp.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YxjnImp.deleteAndFlush(this);
    }
    
    
    
    //--------------------------------Cache 中的索引 GET & SET
	public String getId_Unique(){
		return id_Unique;
	}
	
	public void setId_Unique(String id_Unique){
		this.id_Unique = id_Unique;
	}
	
	public String getYhyx_id_Index(){
		return yhyx_id_Index;
	}
	
	public void setYhyx_id_Index(String yhyx_id_Index){
		this.yhyx_id_Index = yhyx_id_Index;
	}
	
	
	
	/** 取得改变的索引 */
	public Map<String,String> getUpdateIndexs(){
		Map<String,String> indexNames = new HashMap<String,String>();
		if(!this.getId_Unique().equals(SK_Plus.b(id).e())){
			indexNames.put(Yxjn.Id_IndexName,SK_Plus.b(id).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		if(!this.getYhyx_id_Index().equals(SK_Plus.b(yhyx_id).e()))
			indexNames.put(Yxjn.Yhyx_id_IndexName,SK_Plus.b(yhyx_id).e());
			
		return indexNames;
	}
}